home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / POV-Ray 3.0.2 / src / SOURCE / LIBPNG / PNGIO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-26  |  10.1 KB  |  319 lines  |  [TEXT/CWIE]

  1.  
  2. /* pngio.c - stub functions for i/o and memory allocation
  3.  
  4.    libpng 1.0 beta 2 - version 0.88
  5.    For conditions of distribution and use, see copyright notice in png.h
  6.    Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  7.    January 25, 1996
  8.  
  9.    This file provides a location for all input/output.  Users which need
  10.    special handling are expected to write functions which have the same
  11.    arguments as these, and perform similar functions, but possibly have
  12.    different I/O methods.  Note that you shouldn't change these functions,
  13.    but rather write replacement functions and then change them at run
  14.    time with png_set_write_fn(...) or png_set_read_fn(...), etc */
  15.  
  16. #define PNG_INTERNAL
  17. #include "png.h"
  18.  
  19. /* Write the data to whatever output you are using.  The default routine
  20.    writes to a file pointer.  Note that this routine sometimes gets called
  21.    with very small lengths, so you should implement some kind of simple
  22.    buffering if you are using unbuffered writes.  This should never be asked
  23.    to write more then 64K on a 16 bit machine.  The cast to png_size_t is
  24.    there to quiet warnings of certain compilers. */
  25.  
  26. void
  27. png_write_data(png_structp png_ptr, png_bytep data, png_uint_32 length)
  28. {
  29.    if (png_ptr->write_data_fn)
  30.       (*(png_ptr->write_data_fn))(png_ptr, data, length);
  31.    else
  32.       png_error(png_ptr, "Call to NULL write function");
  33. }
  34.  
  35. /* This is the function which does the actual writing of data.  If you are
  36.    not writing to a standard C stream, you should create a replacement
  37.    write_data function and use it at run time with png_set_write_fn(), rather
  38.    than changing the library. */
  39. #ifndef USE_FAR_KEYWORD
  40. void
  41. png_default_write_data(png_structp png_ptr, png_bytep data, png_uint_32 length)
  42. {
  43.    png_uint_32 check;
  44.  
  45.    check = fwrite(data, 1, (png_size_t)length, png_ptr->fp);
  46.    if (check != length)
  47.    {
  48.       png_error(png_ptr, "Write Error");
  49.    }
  50. }
  51. #else
  52. /* this is the model-independent version. Since the standard I/O library
  53.    can't handle far buffers in the medium and small models, we have to copy
  54.    the data.
  55. */
  56.  
  57. #define NEAR_BUF_SIZE 1024
  58. #define MIN(a,b) (a <= b ? a : b)
  59.  
  60. #ifdef _MSC_VER
  61. /* for FP_OFF */
  62. #include <dos.h>
  63. #endif
  64.  
  65. void
  66. png_default_write_data(png_structp png_ptr, png_bytep data, png_uint_32 length)
  67. {
  68.    png_uint_32 check;
  69.    png_byte *n_data;
  70.  
  71.    /* Check if data really is near. If so, use usual code. */
  72. #ifdef _MSC_VER
  73.    /* do it this way just to quiet warning */
  74.    FP_OFF(n_data) = FP_OFF(data);
  75.    if (FP_SEG(n_data) == FP_SEG(data))
  76. #else
  77.    /* this works in MSC also but with lost segment warning */
  78.    n_data = (png_byte *)data;
  79.    if ((png_bytep)n_data == data)
  80. #endif
  81.    {
  82.       check = fwrite(n_data, 1, (png_size_t)length, png_ptr->fp);
  83.    }
  84.    else
  85.    {
  86.       png_byte buf[NEAR_BUF_SIZE];
  87.       png_size_t written, remaining, err;
  88.       check = 0;
  89.       remaining = (png_size_t)length;
  90.       do
  91.       {
  92.          written = MIN(NEAR_BUF_SIZE, remaining);
  93.          png_memcpy(buf, data, written); /* copy far buffer to near buffer */
  94.          err = fwrite(buf, 1, written, png_ptr->fp);
  95.          if (err != written)
  96.             break;
  97.          else
  98.             check += err;
  99.          data += written;
  100.          remaining -= written;
  101.       }
  102.       while (remaining != 0);
  103.    }
  104.    if (check != length)
  105.    {
  106.       png_error(png_ptr, "Write Error");
  107.    }
  108. }
  109.  
  110. #endif
  111.  
  112. /* Read the data from whatever input you are using.  The default routine
  113.    reads from a file pointer.  Note that this routine sometimes gets called
  114.    with very small lengths, so you should implement some kind of simple
  115.    buffering if you are using unbuffered reads.  This should never be asked
  116.    to read more then 64K on a 16 bit machine.  The cast to png_size_t is
  117.    there to quiet some compilers */
  118. void
  119. png_read_data(png_structp png_ptr, png_bytep data, png_uint_32 length)
  120. {
  121. #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
  122.    if (png_ptr->read_mode == PNG_READ_PUSH_MODE)
  123.    {
  124.       png_push_fill_buffer(png_ptr, data, length);
  125.    }
  126.    else
  127. #endif
  128.    {
  129.       if (png_ptr->read_data_fn)
  130.          (*(png_ptr->read_data_fn))(png_ptr, data, length);
  131.       else
  132.          png_error(png_ptr, "Call to NULL read function");
  133.    }
  134. }
  135.  
  136. /* This is the function which does the actual reading of data.  If you are
  137.    not reading from a standard C stream, you should create a replacement
  138.    read_data function and use it at run time with png_set_read_fn(), rather
  139.    than changing the library. */
  140. #ifndef USE_FAR_KEYWORD
  141. void
  142. png_default_read_data(png_structp png_ptr, png_bytep data, png_uint_32 length)
  143. {
  144.    png_uint_32 check;
  145.  
  146.    check = fread(data, 1, (size_t)length, png_ptr->fp);
  147.    if (check != length)
  148.    {
  149.       png_error(png_ptr, "Read Error");
  150.    }
  151. }
  152. #else
  153. void
  154. png_default_read_data(png_structp png_ptr, png_bytep data, png_uint_32 length)
  155. {
  156.    png_uint_32 check;
  157.    png_byte *n_data;
  158.  
  159.    /* Check if data really is near. If so, use usual code. */
  160. #ifdef _MSC_VER
  161.    /* do it this way just to quiet warning */
  162.    FP_OFF(n_data) = FP_OFF(data);
  163.    if (FP_SEG(n_data) == FP_SEG(data))
  164. #else
  165.    /* this works in MSC also but with lost segment warning */
  166.    n_data = (png_byte *)data;
  167.    if ((png_bytep)n_data == data)
  168. #endif
  169.    {
  170.       check = fread(n_data, 1, (size_t)length, png_ptr->fp);
  171.    }
  172.    else
  173.    {
  174.       png_byte buf[NEAR_BUF_SIZE];
  175.       png_size_t read, remaining, err;
  176.       check = 0;
  177.       remaining = (png_size_t)length;
  178.       do
  179.       {
  180.          read = MIN(NEAR_BUF_SIZE, remaining);
  181.          err = fread(buf, 1, read, png_ptr->fp);
  182.          png_memcpy(data, buf, read); /* copy far buffer to near buffer */
  183.          if(err != read)
  184.             break;
  185.          else
  186.             check += err;
  187.          data += read;
  188.          remaining -= read;
  189.       }
  190.       while (remaining != 0);
  191.    }
  192.    if (check != length)
  193.    {
  194.       png_error(png_ptr, "read Error");
  195.    }
  196. }
  197. #endif
  198.  
  199. /* This function is called to output any data pending writing (normally
  200.    to disk.  After png_flush is called, there should be no data pending
  201.    writing in any buffers. */
  202. #if defined(PNG_WRITE_FLUSH_SUPPORTED)
  203. void
  204. png_flush(png_structp png_ptr)
  205. {
  206.    if (png_ptr->output_flush_fn)
  207.       (*(png_ptr->output_flush_fn))(png_ptr);
  208. }
  209.  
  210. void
  211. png_default_flush(png_structp png_ptr)
  212. {
  213.    if (png_ptr->fp)
  214.       fflush(png_ptr->fp);
  215. }
  216. #endif
  217.  
  218. /* This function allows the application to supply new output functions for
  219.    libpng if standard C streams aren't being used.
  220.  
  221.    This function takes as its arguments:
  222.    png_ptr       - pointer to a png output data structure
  223.    io_ptr        - pointer to user supplied structure containing info about
  224.                    the output functions.  May be NULL.
  225.    write_data_fn - pointer to a new output function which takes as its
  226.                    arguments a pointer to a png_struct, a pointer to
  227.                    data to be written, and a 32-bit unsigned int which is
  228.                    the number of bytes to be written.  The new write
  229.                    function should call png_error(png_ptr, "Error msg")
  230.                    to exit and output any fatal error messages.
  231.    flush_data_fn - pointer to a new flush function which takes as its
  232.                    arguments a pointer to a png_struct.  After a call to
  233.                    the flush function, there should be no data in any buffers
  234.                    or pending transmission.  If the output method doesn't do
  235.                    any buffering of ouput, a function prototype must still be
  236.                    supplied although it doesn't have to do anything.  If
  237.                    PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile
  238.                    time, output_flush_fn will be ignored, although it must be
  239.                    supplied for compatibility. */
  240. void
  241. png_set_write_fn(png_structp png_ptr, png_voidp io_ptr,
  242.    png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)
  243. {
  244.    png_ptr->io_ptr = io_ptr;
  245.  
  246.    if (write_data_fn)
  247.       png_ptr->write_data_fn = write_data_fn;
  248.    else
  249.       png_ptr->write_data_fn = png_default_write_data;
  250.  
  251. #if defined(PNG_WRITE_FLUSH_SUPPORTED)
  252.    if (output_flush_fn)
  253.       png_ptr->output_flush_fn = output_flush_fn;
  254.    else
  255.       png_ptr->output_flush_fn = png_default_flush;
  256. #endif /* PNG_WRITE_FLUSH_SUPPORTED */
  257.  
  258.    /* It is an error to read while writing a png file */
  259.    png_ptr->read_data_fn = NULL;
  260. }
  261.  
  262.  
  263. /* This function allows the application to supply a new input function
  264.    for libpng if standard C streams aren't being used.
  265.  
  266.    This function takes as its arguments:
  267.    png_ptr      - pointer to a png input data structure
  268.    io_ptr       - pointer to user supplied structure containing info about
  269.                   the input functions.  May be NULL.
  270.    read_data_fn - pointer to a new input function which takes as it's
  271.                   arguments a pointer to a png_struct, a pointer to
  272.                   a location where input data can be stored, and a 32-bit
  273.                   unsigned int which is the number of bytes to be read.
  274.                   To exit and output any fatal error messages the new write
  275.                   function should call png_error(png_ptr, "Error msg"). */
  276. void
  277. png_set_read_fn(png_structp png_ptr, png_voidp io_ptr,
  278.    png_rw_ptr read_data_fn)
  279. {
  280.    png_ptr->io_ptr = io_ptr;
  281.  
  282.    if (read_data_fn)
  283.       png_ptr->read_data_fn = read_data_fn;
  284.    else
  285.       png_ptr->read_data_fn = png_default_read_data;
  286.  
  287.    /* It is an error to write to a read device */
  288.    png_ptr->write_data_fn = NULL;
  289.  
  290. #if defined(PNG_WRITE_FLUSH_SUPPORTED)
  291.    png_ptr->output_flush_fn = NULL;
  292. #endif /* PNG_WRITE_FLUSH_SUPPORTED */
  293. }
  294.  
  295.  
  296. /* This function returns a pointer to the io_ptr associated with the user
  297.    functions.  The application should free any memory associated with this
  298.    pointer before png_write_destroy and png_read_destroy are called. */
  299. png_voidp
  300. png_get_io_ptr(png_structp png_ptr)
  301. {
  302.    return png_ptr->io_ptr;
  303. }
  304.  
  305. /* Initialize the default input/output functions for the png file.  If you
  306.    change the read, or write routines, you can call either png_set_read_fn()
  307.    or png_set_write_fn() instead of png_init_io(). */
  308. void
  309. png_init_io(png_structp png_ptr, FILE *fp)
  310. {
  311.    png_ptr->fp = fp;
  312.    png_ptr->read_data_fn = png_default_read_data;
  313.    png_ptr->write_data_fn = png_default_write_data;
  314. #ifdef PNG_WRITE_FLUSH_SUPPORTED
  315.    png_ptr->output_flush_fn = png_default_flush;
  316. #endif
  317. }
  318.  
  319.